home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 16 Instancing and Frustum Culling / InstancingAndCulling / Shaders / Default.hlsl next >
Encoding:
Text File  |  2016-03-02  |  5.0 KB  |  172 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //***************************************************************************************
  4.  
  5. // Defaults for number of lights.
  6. #ifndef NUM_DIR_LIGHTS
  7.     #define NUM_DIR_LIGHTS 3
  8. #endif
  9.  
  10. #ifndef NUM_POINT_LIGHTS
  11.     #define NUM_POINT_LIGHTS 0
  12. #endif
  13.  
  14. #ifndef NUM_SPOT_LIGHTS
  15.     #define NUM_SPOT_LIGHTS 0
  16. #endif
  17.  
  18. // Include structures and functions for lighting.
  19. #include "LightingUtil.hlsl"
  20.  
  21. struct InstanceData
  22. {
  23.     float4x4 World;
  24.     float4x4 TexTransform;
  25.     uint     MaterialIndex;
  26.     uint     InstPad0;
  27.     uint     InstPad1;
  28.     uint     InstPad2;
  29. };
  30.  
  31. struct MaterialData
  32. {
  33.     float4   DiffuseAlbedo;
  34.     float3   FresnelR0;
  35.     float    Roughness;
  36.     float4x4 MatTransform;
  37.     uint     DiffuseMapIndex;
  38.     uint     MatPad0;
  39.     uint     MatPad1;
  40.     uint     MatPad2;
  41. };
  42.  
  43. // An array of textures, which is only supported in shader model 5.1+.  Unlike Texture2DArray, the textures
  44. // in this array can be different sizes and formats, making it more flexible than texture arrays.
  45. Texture2D gDiffuseMap[7] : register(t0);
  46.  
  47. // Put in space1, so the texture array does not overlap with these resources.  
  48. // The texture array will occupy registers t0, t1, ..., t6 in space0. 
  49. StructuredBuffer<InstanceData> gInstanceData : register(t0, space1);
  50. StructuredBuffer<MaterialData> gMaterialData : register(t1, space1);
  51.  
  52. SamplerState gsamPointWrap        : register(s0);
  53. SamplerState gsamPointClamp       : register(s1);
  54. SamplerState gsamLinearWrap       : register(s2);
  55. SamplerState gsamLinearClamp      : register(s3);
  56. SamplerState gsamAnisotropicWrap  : register(s4);
  57. SamplerState gsamAnisotropicClamp : register(s5);
  58.  
  59. // Constant data that varies per pass.
  60. cbuffer cbPass : register(b0)
  61. {
  62.     float4x4 gView;
  63.     float4x4 gInvView;
  64.     float4x4 gProj;
  65.     float4x4 gInvProj;
  66.     float4x4 gViewProj;
  67.     float4x4 gInvViewProj;
  68.     float3 gEyePosW;
  69.     float cbPerObjectPad1;
  70.     float2 gRenderTargetSize;
  71.     float2 gInvRenderTargetSize;
  72.     float gNearZ;
  73.     float gFarZ;
  74.     float gTotalTime;
  75.     float gDeltaTime;
  76.     float4 gAmbientLight;
  77.  
  78.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  79.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  80.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  81.     // are spot lights for a maximum of MaxLights per object.
  82.     Light gLights[MaxLights];
  83. };
  84.  
  85. struct VertexIn
  86. {
  87.     float3 PosL    : POSITION;
  88.     float3 NormalL : NORMAL;
  89.     float2 TexC    : TEXCOORD;
  90. };
  91.  
  92. struct VertexOut
  93. {
  94.     float4 PosH    : SV_POSITION;
  95.     float3 PosW    : POSITION;
  96.     float3 NormalW : NORMAL;
  97.     float2 TexC    : TEXCOORD;
  98.     
  99.     // nointerpolation is used so the index is not interpolated 
  100.     // across the triangle.
  101.     nointerpolation uint MatIndex  : MATINDEX;
  102. };
  103.  
  104. VertexOut VS(VertexIn vin, uint instanceID : SV_InstanceID)
  105. {
  106.     VertexOut vout = (VertexOut)0.0f;
  107.     
  108.     // Fetch the instance data.
  109.     InstanceData instData = gInstanceData[instanceID];
  110.     float4x4 world = instData.World;
  111.     float4x4 texTransform = instData.TexTransform;
  112.     uint matIndex = instData.MaterialIndex;
  113.  
  114.     vout.MatIndex = matIndex;
  115.     
  116.     // Fetch the material data.
  117.     MaterialData matData = gMaterialData[matIndex];
  118.     
  119.     // Transform to world space.
  120.     float4 posW = mul(float4(vin.PosL, 1.0f), world);
  121.     vout.PosW = posW.xyz;
  122.  
  123.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  124.     vout.NormalW = mul(vin.NormalL, (float3x3)world);
  125.  
  126.     // Transform to homogeneous clip space.
  127.     vout.PosH = mul(posW, gViewProj);
  128.     
  129.     // Output vertex attributes for interpolation across triangle.
  130.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), texTransform);
  131.     vout.TexC = mul(texC, matData.MatTransform).xy;
  132.     
  133.     return vout;
  134. }
  135.  
  136. float4 PS(VertexOut pin) : SV_Target
  137. {
  138.     // Fetch the material data.
  139.     MaterialData matData = gMaterialData[pin.MatIndex];
  140.     float4 diffuseAlbedo = matData.DiffuseAlbedo;
  141.     float3 fresnelR0 = matData.FresnelR0;
  142.     float  roughness = matData.Roughness;
  143.     uint diffuseTexIndex = matData.DiffuseMapIndex;
  144.     
  145.     // Dynamically look up the texture in the array.
  146.     diffuseAlbedo *= gDiffuseMap[diffuseTexIndex].Sample(gsamLinearWrap, pin.TexC);
  147.     
  148.     // Interpolating normal can unnormalize it, so renormalize it.
  149.     pin.NormalW = normalize(pin.NormalW);
  150.  
  151.     // Vector from point being lit to eye. 
  152.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  153.  
  154.     // Light terms.
  155.     float4 ambient = gAmbientLight*diffuseAlbedo;
  156.  
  157.     const float shininess = 1.0f - roughness;
  158.     Material mat = { diffuseAlbedo, fresnelR0, shininess };
  159.     float3 shadowFactor = 1.0f;
  160.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  161.         pin.NormalW, toEyeW, shadowFactor);
  162.  
  163.     float4 litColor = ambient + directLight;
  164.  
  165.     // Common convention to take alpha from diffuse albedo.
  166.     litColor.a = diffuseAlbedo.a;
  167.  
  168.     return litColor;
  169. }
  170.  
  171.  
  172.